package com.example.myanimator.utils

import android.content.Context

/**
 * @author tgw
 * @date 2021/12/13
 * @describe
 */
object ScreenUtils {

    /**
     * dp转px
     */
    fun dip2px(context: Context, dpValue: Float): Float {
        val scale = context.resources.displayMetrics.density
        return dpValue * scale + 0.5f
    }

    /**
     *px转dp
     */
    fun px2dip(context: Context, pxValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (pxValue / scale + 0.5f).toInt()
    }

    /**
     *px转换为sp
     */
    fun px2sp(context: Context, pxValue: Float): Int {
        val scaledDensity = context.resources.displayMetrics.scaledDensity
        return (pxValue / scaledDensity + 0.5f).toInt()
    }
    /**
     *sp转换为px
     */
    fun sp2px(context: Context, spValue: Float): Int {
        val scaledDensity = context.resources.displayMetrics.scaledDensity
        return (spValue * scaledDensity + 0.5f).toInt()
    }


}